// subpages0/bargainRecords/bargainRecords.js
var app = getApp()
Page({
  data: {
    showLoading: true,
    noMoretip: false,
    page: 0
  },
  onLoad: function (e) {
    var that = this;
    if (e && e.gid) {
      that.setData({
        gid: e.gid
      })
    }
    if (e && e.title) {
      app.setNavtitle(e.title);
    } else {
      app.setNavtitle('预约记录');
    }
    that.requestRecordlist();
    that.requestJoinnum();
  },
  // 已参加预约人数数量
  requestJoinnum: function () {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        goods_id: that.data.gid,
        map: 'applet_appointment_join_count'
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.ec == 200) {
          that.setData({
            joinCount: res.data.data.count
          })
        } else {

        }
      },
      fail: function () {
        console.log("fail")
      },
      complete: function () {

      }
    })
  },
  requestRecordlist: function () {
    var that = this;
    var data = {
      map: 'applet_appointment_join_list',
      goods_id: that.data.gid
    };
    var page = that.data.page;
    data.page = page;
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(page);
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.recordsList ? that.data.recordsList : [];
          var curArr = res.data.data.list;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data.list;
          }
          that.setData({
            recordsList: allArr
          })
          if (lastPageLength < 50) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.recordsList);
        } else {
          if (page <= 0) {
            that.setData({
              recordsList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    that.requestRecordlist();
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestRecordlist();
    }
  },
})